package com.sky.service.impl;

import com.sky.properties.MinioProperties;
import com.sky.service.FileService;
import io.minio.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * ClassName: FileServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 小龙
 * @Create 2025/9/26 5:18
 * @Version 1.0
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd");


    private MinioProperties properties;

    private MinioClient minioClient;
    public FileServiceImpl(MinioProperties properties, MinioClient minioClient) {
        this.properties = properties;
        this.minioClient = minioClient;
    }

    @Override
    public String fileUpload(MultipartFile file) {
        try {
            log.info("MinIO endpoint: {}", properties.getEndpoint());
            log.info("MinIO accessKey: {}", properties.getAccessKey());
            boolean bucketExist = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(properties.getBucketName()).build());
            if (!bucketExist) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(properties.getBucketName()).build());
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                        .bucket(properties.getBucketName())
                        .config(createBucketPolicyConfig(properties.getBucketName()))
                        .build());

            }
            // String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + UUID.randomUUID() + "-" + file.getOriginalFilename();

            String fileName = LocalDate.now().format(DATE_FORMAT) + "/" + UUID.randomUUID()
                    + "-" + file.getOriginalFilename();

            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(properties.getBucketName())
                    .object(fileName)
                    .stream(file.getInputStream(),file.getSize(),-1)
                            .contentType(file.getContentType()).build());
            return String.join("/",properties.getEndpoint(),properties.getBucketName(),fileName);
        } catch (Exception e) {
            log.info("文件上传失败",e);

             e.printStackTrace();
        }
        return null;
    }

    private String createBucketPolicyConfig(String bucketName) {
        return String.format(
                "{\n" +
                        "  \"Statement\": [{\n" +
                        "    \"Action\": \"s3:GetObject\",\n" +
                        "    \"Effect\": \"Allow\",\n" +
                        "    \"Principal\": \"*\",\n" +
                        "    \"Resource\": \"arn:aws:s3:::%s/*\"\n" +
                        "  }],\n" +
                        "  \"Version\": \"2012-10-17\"\n" +
                        "}", bucketName);
    }
}
